public class Solution {
    public int countSubstrings(String s) {
        int len = s.length();
        char[] ch = s.toCharArray();

        //以i为开始、j为结尾的字串是否是回文字串
        boolean[][] dp = new boolean[len][len];

        for (int i = len - 1; i >= 0; i--) {
            for (int j = len - 1; j >= i; j--) {
                if (ch[i] != ch[j]) {
                    dp[i][j] = false;
                } else {
                    if (i == j || i + 1 == j) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                }
            }
        }

        int ret = 0;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if (dp[i][j]) {
                    ret++;
                }
            }
        }
        return ret;
    }
}
